<?php

define('SY_DIR', dirname(__file__));

require_once('functions.php');

require_once('kernel/Simplify.php');

sy_autoload_register('sy_autoload');

uses('kernel');
uses('kernel/core');
uses('kernel/controller');
uses('kernel/data');
uses('kernel/view');
uses('kernel/db');

$config = s::config();

$config['app_dir'] = APP_DIR;
$config['app_url'] = s::request()->url();

$config['cache_dir'] = 'asdf/{app_dir}/cache';

$config['views_dir'] = '{app_dir}/views/{theme}';
$config['views_cache_dir'] = '{cache_dir}/views';

$config['public_path'] = '/public';

$config['www_dir'] = dirname($_SERVER['SCRIPT_FILENAME']) . '{public_path}';
$config['www_url'] = '{app_url}{public_path}';

$config['files_dir'] = '{www_dir}/files';
$config['files_url'] = '{www_url}/files';

$config['theme'] = 'default';
$config['theme_url'] = '{www_url}/{theme}';

$config['admin_url'] = '{app_url}/admin';

if (file_exists(APP_DIR . '/config.php')) {
  require_once(APP_DIR . '/config.php');
}

require_once('debug.php');

function uses($path)
{
  global $includePath;

  $includePath[] = $path;

  if (! sy_path_is_absolute($path)) {
    $path = SY_DIR . '/' . $path;
  }

  if (! sy_path_is_absolute($path)) {
    $path = SY_DIR . '/' . $path;
  }

  if (file_exists($path . '/exceptions.php')) {
    require_once($path . '/exceptions.php');
  }

  if (file_exists($path . '/functions.php')) {
    require_once($path . '/functions.php');
  }
}

function sy_autoload($class)
{
  global $includePath;

  $filename = $class . '.php';

  foreach ($includePath as $path) {
    if (! sy_path_is_absolute($path)) {
      $path = SY_DIR . '/' . $path;
    }

    if (strpos($class, 'I') === 0 && file_exists($path . '/api/' . $filename)) {
      $filename = 'api/' . $filename;
    }

    if (file_exists($path . '/' . $filename)) {
      require_once($path . '/' . $filename);

      break;
    }
  }
}
